package org.davidliebman.android.awesomecube;


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;


import android.opengl.Matrix;
import android.util.Log;


public class Picking {
	//vec2 mwp = new vec2();
	GL10 gl;
	
	public static final int INDEX_SPAN = 20;
	public static final float DEVIDE = 255.0f;
	public static final int MARGIN = 10;
	
	public Picking(GL10 gl) {
		this.gl = gl;
		
	}
	
	    public static int readColor(GL10 gl, int x, int y) {
	    	
	    	
	    	
	    	ByteBuffer PixelBuffer = ByteBuffer.allocateDirect(4); 
			PixelBuffer.order(ByteOrder.nativeOrder()); 
			PixelBuffer.position(0);
			int mTemp = 0;
			gl.glReadPixels((int)x, (int) y, 1, 1, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, PixelBuffer);
			byte b [] = new byte[4];
			PixelBuffer.get(b);
			
			//mTemp = PixelBuffer.get(0);
			mTemp = b[0];
			if (mTemp < 0) mTemp = mTemp + 256	;
			
			return mTemp;
	    }
	    
	    public static int thresholdUpper(int i) {
	    	return getIdNum(i) + MARGIN;
	    }
	    public static int thresholdLower (int i) {
	    	return getIdNum(i) - MARGIN;
	    }
	    
	    public static int getIdNum(int i) {
	    	return (i + 1)  * Picking.INDEX_SPAN;
	    }
}

